package com.android.dopewars;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class Store extends Activity {
	
	public class BuyGunListener implements View.OnClickListener {

		public BuyGunListener(int gun_index, Button gun_button) {
			gun_index_ = gun_index;
			gun_button_ = gun_button;
		}
		public void onClick(View v) {
			if (Global.gun_available[gun_index_] && Global.dealer_.cash() > Global.available_guns_.elementAt(gun_index_).price()) {
    			Global.dealer_.setCash(Global.dealer_.cash() - 
    					Global.available_guns_.elementAt(gun_index_).price());
    			Global.dealer_.addGun(new Gun(Global.available_guns_.elementAt(gun_index_)));
    		}
			Global.gun_available[gun_index_] = false;
			gun_button_.setVisibility(View.GONE);
		}
		
		int gun_index_;
		Button gun_button_;
	}
	
	public class BuyCoatListener implements View.OnClickListener {

		public BuyCoatListener(int coat_index, Button coat_button) {
			coat_index_ = coat_index;
			coat_button_ = coat_button;
		}
		public void onClick(View v) {
			if (Global.coat_available[coat_index_] && Global.dealer_.cash() > Global.available_bag_prices_.elementAt(coat_index_)) {
        		Global.dealer_.setCash(Global.dealer_.cash() - Global.available_bag_prices_.elementAt(coat_index_));
       			Global.dealer_.setTotalSpace(Global.dealer_.total_space() + Global.available_bag_sizes_.elementAt(coat_index_));
        	}
			Global.coat_available[coat_index_] = false;
			coat_button_.setVisibility(View.GONE);
		}
		
		int coat_index_;
		Button coat_button_;
	}
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.store);
        
        LinearLayout guns = (LinearLayout)findViewById(R.id.gun_container);
        TextView gun_title = (TextView)findViewById(R.id.gun_title);
        gun_title.setVisibility(View.GONE);
        for (int i = 0; i < Global.gun_available.length; ++i) {
        	if (Global.gun_available[i]) {
        		Button gun_button = new Button(this, null, android.R.attr.buttonStyleSmall);
        		gun_button.setLayoutParams(new LinearLayout.LayoutParams(
        				TableRow.LayoutParams.FILL_PARENT,
        				TableRow.LayoutParams.WRAP_CONTENT));
        		gun_button.setText(Global.available_guns_.elementAt(i).name() + ": $" + Integer.toString(Global.available_guns_.elementAt(i).price()));
        		gun_button.setOnClickListener(new BuyGunListener(i, gun_button));
        		gun_button.setEnabled(Global.dealer_.cash() > Global.available_guns_.elementAt(i).price());
        		guns.addView(gun_button);
        		gun_title.setVisibility(View.VISIBLE);
        	}
        }
        
        LinearLayout coats = (LinearLayout)findViewById(R.id.coat_container);
        TextView coat_title = (TextView)findViewById(R.id.coats_title);
        coat_title.setVisibility(View.GONE);
        for (int i = 0; i < Global.coat_available.length; ++i) {
        	if (Global.coat_available[i]) {
        		Button coat_button = new Button(this, null, android.R.attr.buttonStyleSmall);
        		coat_button.setLayoutParams(new LinearLayout.LayoutParams(
        				TableRow.LayoutParams.FILL_PARENT,
        				TableRow.LayoutParams.WRAP_CONTENT));
        		coat_button.setText(Global.available_bag_names_.elementAt(i) + " [+" +
        				Integer.toString(Global.available_bag_sizes_.elementAt(i)) + "]: $" +
        				Integer.toString(Global.available_bag_prices_.elementAt(i)));
        		coat_button.setOnClickListener(new BuyCoatListener(i, coat_button));
        		coat_button.setEnabled(Global.dealer_.cash() > Global.available_bag_prices_.elementAt(i));
        		coats.addView(coat_button);
        		coat_title.setVisibility(View.VISIBLE);
        	}
        }
        
        ((Button)findViewById(R.id.ok_button)).setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		Intent i = new Intent();
        		setResult(RESULT_OK, i);
        		finish();
        	}
        });
        
        WindowManager.LayoutParams dialog_params = getWindow().getAttributes();
    	dialog_params.width = WindowManager.LayoutParams.FILL_PARENT;
    	getWindow().setAttributes(dialog_params);
    }
}
